/*
 * ResultSetTypeDemo.java
 *
 * Created on 7 de abril de 2005, 15:05
 */

package jdbc_04;
import jdbc_01.Conexion;

public class ResultSetTypeDemo {
    
    Conexion con;
    
    ResultSetTypeDemo() {
        Conexion.driverOdbc();
        con = new Conexion("empleados", "empleados", "empleados");
        if (con.abrirConexion()) {
            verDatos();
            con.cerrarConexion();
        }
    }
    
    void verDatos(){
        javax.swing.JTextArea jta = new javax.swing.JTextArea(10,40);
        try {
            java.sql.Statement stmt = con.getCon().createStatement(
                    java.sql.ResultSet.TYPE_SCROLL_SENSITIVE, 
                    java.sql.ResultSet.CONCUR_READ_ONLY);

            java.sql.ResultSet rset = stmt.executeQuery("Select * from empleados");
            java.sql.ResultSetMetaData rsmd = rset.getMetaData();
            
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                jta.append(rsmd.getColumnName(i).toUpperCase());
                if (rsmd.getColumnName(i).equals("nacimiento")) jta.append("\t");
                jta.append("\t");
            }
            jta.append("\n");
            int i;
            rset.afterLast();
            while( rset.previous() ) {
                for ( i = 1; i <= rsmd.getColumnCount(); i++) {
                    Object o = rset.getObject(i);
                    if ( o != null )
                        jta.append(o.toString().trim());
                    else 
                        jta.append("\t");
                    jta.append("\t");
                }
                jta.append("\n");
            }
            rset.close();    
            stmt.close();
        } catch (java.sql.SQLException e) {
            jta.setText(e.getMessage());
        } finally {
            javax.swing.JOptionPane.showMessageDialog(null, 
                                new javax.swing.JScrollPane(jta));
        }
    }
    
    public static void main(String[] args) {
        new ResultSetTypeDemo();
        System.exit(0);
    }
    
}
